clear
use dta/KDB1924_55674.DTA 

gen knr=v1
rename v1 knavn
rename v2 eligiblemen
rename v3 eligiblewomen
rename v4 eligibleoverall
rename v5 eligiblesuspmen
rename v6 eligiblesuspwomen
rename v7 personalvotesmen
rename v8 personalvotewomen
rename v9 personalvoteoverall
rename v10 mailmen
rename v11 mailwomen
rename v12 mailoverall
drop v13 
rename v14 castedvotesoverall
rename v15 rejectedvotesoverall
rename v16 approvedvotesoverall
rename v17 votesNKP
rename v18 votesDNA
rename v19 votesNSA
rename v20 votesRF
rename v21 votesV
rename v22 votesSP 
rename v23 votesFV
rename v24 votesH_FV
rename v25 votesOTH
rename v26 votesVIL

/* COUNTIES */

g cnr = 0
replace cnr =1 if knr<200
replace cnr =2 if knr>200 & knr<300
replace cnr =3 if knr==301
replace cnr =4 if knr>400 & knr<500
replace cnr =5 if knr>500 & knr<600
replace cnr =6 if knr>600 & knr<700
replace cnr =7 if knr>700 & knr<800
replace cnr =8 if knr>800 & knr<900
replace cnr =9 if knr>900 & knr<1000
replace cnr =10 if knr>1000 & knr<1100
replace cnr =11 if knr>1100 & knr<1200
replace cnr =12 if knr>1200 & knr<1300
replace cnr =13 if knr>1300 & knr<1400
replace cnr =14 if knr>1400 & knr<1500
replace cnr =15 if knr>1500 & knr<1600
replace cnr =16 if knr>1600 & knr<1700
replace cnr =17 if knr>1700 & knr<1800
replace cnr =18 if knr>1800 & knr<1900
replace cnr =19 if knr>1900 & knr<2000
replace cnr =20 if knr>2000 

/* VALGKRETS */

g valgkrets = 0
replace valgkrets =1 if knr<200
replace valgkrets =2 if knr>200 & knr<300
replace valgkrets =3 if knr==301
replace valgkrets =4 if knr>400 & knr<500
replace valgkrets =5 if knr>500 & knr<600
replace valgkrets =6 if knr>600 & knr<700
replace valgkrets =7 if knr>700 & knr<800
replace valgkrets =8 if knr>800 & knr<900
replace valgkrets =9 if knr>900 & knr<1000
replace valgkrets =10 if knr>1000 & knr<1100
replace valgkrets =11 if knr>1100 & knr<1200
replace valgkrets =12 if knr>1200 & knr<1300
replace valgkrets =13 if knr>1300 & knr<1400
replace valgkrets =14 if knr>1400 & knr<1500
replace valgkrets =15 if knr>1500 & knr<1600
replace valgkrets =16 if knr>1600 & knr<1700
replace valgkrets =17 if knr>1700 & knr<1800
replace valgkrets =18 if knr>1800 & knr<1900
replace valgkrets =19 if knr>1900 & knr<2000
replace valgkrets =20 if knr>2000 


*** KJOPSTEDER

/* 4 REP */
replace valgkrets=21 if knr==101  /* fredrikshald */  
replace valgkrets=21 if knr==102  /* sarpsborg */
replace valgkrets=21 if knr==103  /* fredrikstad */
replace valgkrets=21 if knr==104  /* moss */
replace valgkrets=21 if knr==203  /* drøbak */
/* 7 REP */
replace valgkrets=22 if knr==301  /* oslo  */  
/* 3 REP */
replace valgkrets=23 if knr==401  /* hamar */   
replace valgkrets=23 if knr==402  /* kongsvinger */
replace valgkrets=23 if knr==501  /* lillehammer */
replace valgkrets=23 if knr==502  /* gjøvik */
/* 3 REP */
replace valgkrets=24 if knr==602 /* drammen */
replace valgkrets=24 if knr==604  /* kongsberg */
replace valgkrets=24 if knr==601  /* hønefoss */
/* 4 REP */
replace valgkrets=25 if knr==703  /* horten */
replace valgkrets=25 if knr==705  /* tønsberg */
replace valgkrets=25 if knr==706  /* sandefjord */
replace valgkrets=25 if knr==707  /* larvik */
replace valgkrets=25 if knr==702  /* holmestrand */
/* 5 REP */
replace valgkrets=26 if knr==807  /* notodden */
replace valgkrets=26 if knr==806  /* skien */
replace valgkrets=26 if knr==805  /* porsgrund */
replace valgkrets=26 if knr==804  /* brevik */
replace valgkrets=26 if knr==801  /* kragerø */
replace valgkrets=26 if knr==901  /* risør */
replace valgkrets=26 if knr==903  /* arendal */
replace valgkrets=26 if knr==904  /* grimstad */
/* 7 REP */
replace valgkrets=27 if knr==1001 /* kristiansand */
replace valgkrets=27 if knr==1002 /* mandal */
replace valgkrets=27 if knr==1004 /* flekkefjord */
replace valgkrets=27 if knr==1103 /* stavanger */
replace valgkrets=27 if knr==1106 /* haugesund */
/* 5 REP */
replace valgkrets=28 if knr==1301 /* bergen */
/* 3 REP */
replace valgkrets=29 if knr==1502 /* molde */
replace valgkrets=29 if knr==1501 /* ålesund */
replace valgkrets=29 if knr==1503 /* kristiansund */
/* 5 REP */
replace valgkrets=30 if knr==1601 /* trondheim */
replace valgkrets=30 if knr==1701 /* levanger */
/* 4 REP */
replace valgkrets=31 if knr==1804 /* bodoe */
replace valgkrets=31 if knr==1805 /* narvik */
replace valgkrets=31 if knr==1902 /* tromsoe */
replace valgkrets=31 if knr==2002 /* vardoe */
replace valgkrets=31 if knr==2003 /* vadsoe */
replace valgkrets=31 if knr==2001 /* hammerfest */

gen rep=0
replace rep=6 if valgkrets==1
replace rep=7 if valgkrets==2
replace rep=7 if valgkrets==4
replace rep=6 if valgkrets==5
replace rep=5 if valgkrets==6
replace rep=4 if valgkrets==7
replace rep=5 if valgkrets==8
replace rep=4 if valgkrets==9
replace rep=4 if valgkrets==10
replace rep=5 if valgkrets==11
replace rep=8 if valgkrets==12
replace rep=5 if valgkrets==14
replace rep=7 if valgkrets==15
replace rep=6 if valgkrets==16
replace rep=5 if valgkrets==17
replace rep=8 if valgkrets==18
replace rep=5 if valgkrets==19
replace rep=3 if valgkrets==20
replace rep=4 if valgkrets==21
replace rep=7 if valgkrets==22
replace rep=3 if valgkrets==23
replace rep=3 if valgkrets==24
replace rep=4 if valgkrets==25
replace rep=5 if valgkrets==26
replace rep=7 if valgkrets==27
replace rep=5 if valgkrets==28
replace rep=3 if valgkrets==29
replace rep=5 if valgkrets==30
replace rep=4 if valgkrets==31



**************** UPDATE OCTOBER 2017 - FIXING ERRORS IN MUNICIPALITY LEVEL DATA  - originally only fixed for the SMD level data ***************************


replace votesH_FV=1863 if knr ==1501  	
replace votesH_FV=479 if knr ==1502  	
replace votesH_FV=0 if knr ==1503  	
replace votesOTH=0 if knr ==1501 
replace votesOTH=0 if knr ==1502 
replace votesOTH=3348 if knr ==1503   /* apparently no votes for KS outside Kristiansund */


**adding "Nordfjords Venstre" to OTH
replace votesOTH=1 if knr==1421
replace votesOTH=2 if knr==1418
replace votesOTH=1 if knr==1441 
replace votesOTH=25 if knr==1439
replace votesOTH=1 if knr==1440
replace votesOTH=244 if knr==1442
replace votesOTH=677 if knr==1443
replace votesOTH=185 if knr==1444
replace votesOTH=1 if knr==1445
replace votesOTH=167 if knr==1446
replace votesOTH=66 if knr==1447
replace votesOTH=199 if knr==1448
replace votesOTH=9 if knr==1401

**subtracting "Nordfjords Venstre" from V

replace votesV=votesV-votesOTH if valgkrets==14


/* MUNICIPALITY LEVEL FILE */
gen year=1924
sort knr
save dta/Storting1924_municipality, replace

/* CREATING DATA SET ON THE VALGKRETS LEVEL */

collapse (sum) eligible* casted* approved* votes* (mean) rep, by(valgkrets)


/* KRISTIANDSUND SAMLINGSPARTI (3348 votes) ERROUNOUSLY INCLUDED IN H AND FV (listed as 5690) - discovered March 5 2014*/

replace votesH_FV=2342 if valgkrets==29
replace votesOTH=3348 if valgkrets==29

/* nordjord venstre (1578 votes) ERROUNOUSLY INCLUDED IN v (listed as 13469 ) */

replace votesV=11891 if valgkrets==14
replace votesOTH=1578 if valgkrets==14


/* CREATING TURNOUT AND VOTE SHARE VARIABLES */

gen turnout=castedvotesoverall/eligibleoverall

foreach party in NKP DNA NSA RF V SP FV H_FV OTH VIL{
gen votesh`party'=votes`party'/approvedvotesoverall
}

gen year=1924

** what we have is "stemmesedler" , it appears that it is possible to make alterations to the lists, resulting in "listestemmer"
** "listestemmer" is what is used for the allocation of seats 

sort year valgkrets
save dta/Storting1924, replace
